'use strict';

const Controller = require('egg').Controller;

class LoginController extends Controller {
  /**
     * @api {POST} /api/login 登录
     * @apiGroup 登录登出
     * @apiDescription 获取登录token, 后续请求时需要在请求头里带上"authorization"，类似"authorization": "3d9d1a155d7a6d2c84fae7bd9ba35466"
     * @apiParam {String} phone 手机号码
     * @apiParam {String} password 密码
     * @apiParamExample 参数模版
     {
       "phone":"13631130000",
       "password":"123456"
     }
     *
     *
     * @apiSuccessExample  成功返回
     {
        "msg": "成功操作",
        "data": {
            "token": "3240918fd044587822443ae7719cd4a3"
        }
     }
     * @apiErrorExample 失败返回
     * {
     *    "msg": "账号或密码错误"
     * }
     */

  async login() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'phone', 'password').checkDataRes) {
      ctx.body = await service.shop.login.login(dataObj);
    }
  }
  // async loginAdmin() {
  //   const { ctx, service } = this;
  //   const dataObj = ctx.request.body;
  //   if (new ctx.helper.checkData(this, 'phone', 'password').checkDataRes) {
  //     ctx.body = await service.shop.login.loginAdmin(dataObj);
  //   }
  // }
  /**
     * @api {Post} /api/logout 退出登录
     * @apiGroup 登录登出
     *
     *
     * @apiSuccessExample  成功返回
     {
       data: "退出成功",
     }
     *
     */

  async logout() {
    const { ctx } = this;
    ctx.session[ctx.request.headers['authorization']] = null;
    ctx.body = this.ctx.response.success('退出成功');
  }
}

module.exports = LoginController;
